package org.one.afternoon.quartz;

import com.baomidou.dynamic.datasource.annotation.DS;
import org.one.afternoon.domain.Client;
import org.one.afternoon.mock.MockHealthJob;
import org.one.afternoon.mock.MockSignJob;
import org.one.afternoon.service.ClientService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Component
public class StartApplicationListener implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private Scheduler scheduler;

    private final ClientService clientService;

    @Autowired
    public StartApplicationListener(ClientService clientService) {
        this.clientService = clientService;
    }

    private JobDataMap getIpMap(){
        //创建Job所需的IP数据用于请求
        List<Client> list = clientService.list(null);
        List<String> ipList = list.stream().map(Client::getIpAddress).toList();
        JobDataMap ipMap = new JobDataMap();
        for(int i=0;i<ipList.size();i++){
            ipMap.put(String.valueOf(i),ipList.get(i));
        }
        return ipMap;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        TriggerKey healthTriggerKey = TriggerKey.triggerKey("healthTrigger", "group");
        TriggerKey testTriggerKey = TriggerKey.triggerKey("testTrigger", "group");
        TriggerKey signTriggerKey = TriggerKey.triggerKey("signTrigger", "group");
        try {
            JobDataMap ipMap = getIpMap();
            //用于调试quartz
//            Trigger testTrigger = scheduler.getTrigger(testTriggerKey);
//            if (testTrigger == null) {
//                testTrigger = TriggerBuilder.newTrigger()
//                        .withIdentity(testTriggerKey)
//                        .withSchedule(CronScheduleBuilder.cronSchedule("0 0 */1 * * ?")
//                                .withMisfireHandlingInstructionDoNothing())
//                        .startAt(DateBuilder.futureDate(100,DateBuilder.IntervalUnit.SECOND))
//                        .build();
//                JobDetail jobDetail=JobBuilder.newJob(MockSignJob.class)
//                        .withIdentity("mockJob","mock_group")
//                        .usingJobData(ipMap).storeDurably(true)
//                        .build();
//                scheduler.scheduleJob(jobDetail,testTrigger);
//                scheduler.addJob(jobDetail,true);
//            }


            //health任务
            Trigger healthTrigger = scheduler.getTrigger(healthTriggerKey);
            if (healthTrigger == null) {
                healthTrigger = TriggerBuilder.newTrigger()
                        .withIdentity(healthTriggerKey)
                        .withSchedule(CronScheduleBuilder.cronSchedule("1 * */1 * * ?")
                                .withMisfireHandlingInstructionDoNothing())
                        .startAt(DateBuilder.futureDate(100, DateBuilder.IntervalUnit.SECOND))
                        .build();
                JobDetail jobDetail = JobBuilder.newJob(HealthJob.class)
                        .withIdentity("healthJob", "health_group")
                        .usingJobData(ipMap).storeDurably(true)
                        .build();
                scheduler.scheduleJob(jobDetail, healthTrigger);
                scheduler.addJob(jobDetail, true);
            }



            //sign任务
            Trigger signTrigger = scheduler.getTrigger(signTriggerKey);
            if (signTrigger == null) {
                signTrigger = TriggerBuilder.newTrigger()
                        .withIdentity(signTriggerKey)
                        .withSchedule(CronScheduleBuilder.cronSchedule("1 * */1 * * ?")
                                .withMisfireHandlingInstructionDoNothing())
                        .startAt(DateBuilder.futureDate(100,DateBuilder.IntervalUnit.SECOND))
                        .build();
                JobDetail jobDetail=JobBuilder.newJob(SignJob.class)
                        .withIdentity("signJob","sign_group")
                        .usingJobData(ipMap).storeDurably(true)
                        .build();
                scheduler.scheduleJob(jobDetail,signTrigger);
                scheduler.addJob(jobDetail,true);
            }
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}
